package com.cicigodd.brick.plug.system.dict.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cicigodd.brick.plug.common.exception.BusinessException;
import com.cicigodd.brick.plug.common.factory.PageFactory;
import com.cicigodd.brick.plug.common.factory.PageResultFactory;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicigodd.brick.plug.system.dict.entity.SysDict;
import com.cicigodd.brick.plug.system.dict.mapper.SysDictMapper;
import com.cicigodd.brick.plug.system.dict.request.*;
import com.cicigodd.brick.plug.system.dict.response.SysDictDetailResponse;
import com.cicigodd.brick.plug.system.dict.response.SysDictListResponse;
import com.cicigodd.brick.plug.system.dict.service.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

/**
 * 系统字典类型
 *
 * @author cicigodd
 * @date 2022/12/12 13:27:02
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    /**
     * 添加
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysDictAddRequest sysDictAddRequest) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDict::getDictType, sysDictAddRequest.getDictType());
        SysDict dict = this.getOne(wrapper);
        if(ObjectUtil.isNotNull(dict)){
            throw new BusinessException("字典编码已存在,请重新输入");
        }
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(sysDictAddRequest, sysDict);
        this.save(sysDict);
    }

    /**
     * 删除
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysDictDeleteRequest sysDictDeleteRequest) {
        this.removeByIds(sysDictDeleteRequest.getIds());
    }


    /**
     * 编辑
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysDictEditRequest sysDictEditRequest) {
        SysDict sysDict = this.getById(sysDictEditRequest.getId());
        if (ObjectUtil.isEmpty(sysDict)) {
            throw new BusinessException("查询结果不存在");
        }
        if(!sysDict.getDictType().equals(sysDictEditRequest.getDictType())){
            LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysDict::getDictType, sysDictEditRequest.getDictType());
            SysDict dict = this.getOne(wrapper);
            if(ObjectUtil.isNotNull(dict)){
                throw new BusinessException("字典编码已存在,请重新输入");
            }
        }

        BeanUtil.copyProperties(sysDictEditRequest, sysDict);
        this.updateById(sysDict);
    }

    /**
     * 详情
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    public SysDictDetailResponse detail(SysDictDetailRequest sysDictDetailRequest) {
        SysDict sysDict = this.getById(sysDictDetailRequest.getId());
        if (ObjectUtil.isEmpty(sysDict)) {
            throw new BusinessException("查询结果不存在");
        }
        SysDictDetailResponse sysDictDetailResponse = new SysDictDetailResponse();
        BeanUtil.copyProperties(sysDict, sysDictDetailResponse);
        return sysDictDetailResponse;
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    public PageResult<SysDictListResponse> list(SysDictListRequest sysDictListRequest) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        String dictType = sysDictListRequest.getDictType();
        String dictTypeLabel = sysDictListRequest.getDictTypeLabel();
        String remark = sysDictListRequest.getRemark();
        Integer sort = sysDictListRequest.getSort();

        wrapper.like(ObjectUtil.isNotEmpty(dictType), SysDict::getDictType, dictType);
        wrapper.like(ObjectUtil.isNotEmpty(dictTypeLabel), SysDict::getDictTypeLabel, dictTypeLabel);
        wrapper.like(ObjectUtil.isNotEmpty(remark), SysDict::getRemark, remark);
        wrapper.eq(ObjectUtil.isNotNull(sort), SysDict::getSort, sort);
        IPage<SysDictListResponse> page = this.page(PageFactory.defaultPage(sysDictListRequest.getPageNum(),sysDictListRequest.getPageSize()), wrapper).convert(SysDict -> BeanUtil.copyProperties(SysDict, SysDictListResponse.class));
        return PageResultFactory.createPageResult(page);
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/12/12 13:27:02
     */
    @Override
    public List<SysDictListResponse> listAll(SysDictListRequest sysDictListRequest) {
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        String dictType = sysDictListRequest.getDictType();
        String dictTypeLabel = sysDictListRequest.getDictTypeLabel();
        String remark = sysDictListRequest.getRemark();

        wrapper.like(ObjectUtil.isNotEmpty(dictType), SysDict::getDictType, dictType);
        wrapper.like(ObjectUtil.isNotEmpty(dictTypeLabel), SysDict::getDictTypeLabel, dictTypeLabel);
        wrapper.like(ObjectUtil.isNotEmpty(remark), SysDict::getRemark, remark);
        List<SysDict> list = this.list(wrapper);
        return BeanUtil.copyToList(list,SysDictListResponse.class);
    }


}
